package controller

import (
	"errors"
	"github.com/e421083458/gin_scaffold/dao"
	"github.com/e421083458/gin_scaffold/dto"
	"github.com/e421083458/gin_scaffold/middleware"
	"github.com/e421083458/gin_scaffold/public"
	"github.com/e421083458/golang_common/lib"
	"github.com/garyburd/redigo/redis"
	"github.com/gin-gonic/gin"
	"strconv"
)
//需要做jwt
type DemoController struct {
}

func DemoRegister(router *gin.RouterGroup) {
	demo := DemoController{}
	//router.GET("/index", demo.Index)
	//router.Any("/bind", demo.Bind)
	//router.GET("/dao", demo.Dao)
	//router.GET("/redis", demo.Redis)
	//router.GET("/update", demo.Update)
	router.GET("/geturl", demo.GetUrl) //获取网站地址
	router.GET("/login", demo.Login) //登录
	router.GET("/change_sk", demo.ChangSk) //收款码短信
	router.GET("/pwd", demo.Pwd) //获取短信
}
func (demo *DemoController) Pwd(c *gin.Context) {
	params := &dto.Scode{}
	if err := params.BindingValidParams(c); err != nil {
		middleware.ResponseError(c, 2001, err)
		return
	}
	code :=c.Query("code")
	mobile :=c.Query("mobile")

	int10, err := strconv.ParseInt(code, 10, 32)
	if err != nil {
		var err error = errors.New("code类型为string")
		middleware.ResponseError(c, 2000, err)
		return
	} else {
		cc := int32(int10)
		s,err:=public.GetCode(mobile,"c",cc,c)
		if err!=true{
			var err error = errors.New(s)
			middleware.ResponseError(c, 2000, err)
			return
		}
		middleware.ResponseSuccess(c, s)
	}

}

func (demo *DemoController) ChangSk(c *gin.Context) {
	tx, err := lib.GetGormPool("default")
	if err != nil {
		middleware.ResponseError(c, 2000, err)
		return
	}
	params := &dto.Change{}
	if err := params.BindingValidParams(c); err != nil {
		middleware.ResponseError(c, 2001, err)
		return
	}
	mobile:=params.Mobile
	uid:=params.Uid

	var userinfo *dao.Users

	userinfo,err = (&dao.Users{}).FindUser(c, tx,uid)
	//验证手机是否一致
	//fmt.Println(userinfo.Username)
	if userinfo.Username!=""{
		if userinfo.Username!=mobile {
			var err error = errors.New("账号不匹配")
			middleware.ResponseError(c, 2000, err)
			return
		}
	}else{
		middleware.ResponseError(c, 2000, err)
			return
	}
	smoke :=public.RandCode()
	i,st:=public.Sms(mobile,smoke)
	if i==1{
		public.SetCode(mobile,"c", smoke,c)
		middleware.ResponseSuccess(c, st)
		return
	}else{
		middleware.ResponseError(c, 2000, st)
		return
	}
}

func (demo *DemoController) Login(c *gin.Context) {
	tx, err := lib.GetGormPool("default")
	if err != nil {
		middleware.ResponseError(c, 2000, err)
		return
	}
	params := &dto.Users{}
	if err := params.BindingValidParams(c); err != nil {
		middleware.ResponseError(c, 2001, err)
		return
	}

	err,user,token := (&dao.Users{}).Login(c, tx,params.Username,params.Password)

	if err != nil {
		middleware.ResponseError(c, 2001, err)
		return
	}
	user.Token=token
	middleware.ResponseSuccess(c, user)
	return
}
func (demo *DemoController) GetUrl(c *gin.Context) {
	middleware.ResponseSuccess(c, "http://whmingbiao.com")
	return
}

func (demo *DemoController) Index(c *gin.Context) {
	middleware.ResponseSuccess(c, "")
	return
}

func (demo *DemoController) Dao(c *gin.Context) {
	tx, err := lib.GetGormPool("default")
	if err != nil {
		middleware.ResponseError(c, 2000, err)
		return
	}
	area, err := (&dao.Area{}).Find(c, tx, c.DefaultQuery("id", "1"))
	if err != nil {
		middleware.ResponseError(c, 2001, err)
		return
	}
	middleware.ResponseSuccess(c, area)
	return
}
func (demo *DemoController) Update(c *gin.Context) {
	tx, err := lib.GetGormPool("default")
	if err != nil {
		middleware.ResponseError(c, 2000, err)
		return
	}
	area, err := (&dao.Area{}).Update(c, tx, c.DefaultQuery("id", "1"),"222")
	if err != nil {
		middleware.ResponseError(c, 2001, err)
		return
	}
	middleware.ResponseSuccess(c, area)
	return
}
func (demo *DemoController) Redis(c *gin.Context) {
	redisKey := "redis_key"
	lib.RedisConfDo(public.GetTraceContext(c),
		"default",
		"SET",
		redisKey, "redis_value")
	redisValue, err := redis.String(
		lib.RedisConfDo(public.GetTraceContext(c), "default",
			"GET",
			redisKey))
	if err != nil {
		middleware.ResponseError(c, 2001, err)
		return
	}
	middleware.ResponseSuccess(c, redisValue)
	return
}

// ListPage godoc
// @Summary 测试数据绑定
// @Description 测试数据绑定
// @Tags 用户
// @ID /demo/bind
// @Accept  json
// @Produce  json
// @Param polygon body dto.DemoInput true "body"
// @Success 200 {object} middleware.Response{data=dto.DemoInput} "success"
// @Router /demo/bind [post]
func (demo *DemoController) Bind(c *gin.Context) {
	params := &dto.DemoInput{}
	if err := params.BindingValidParams(c); err != nil {
		middleware.ResponseError(c, 2001, err)
		return
	}
	middleware.ResponseSuccess(c, params)
	return
}